using System;
using Microsoft.SPOT;
using System.Net;
using System.IO;
using System.Collections;
using Komodex.NETMF.MicroTweet.HTTP;

namespace Komodex.NETMF.MicroTweet
{
    // MicroTweet
    // Matt Isenhower, Komodex Systems LLC
    // http://microtweet.codeplex.com

    public partial class TwitterClient
    {
        #region Sending a Tweet

        public bool SendTweet(string message)
        {
            const string sendTweetUrl = "http://api.twitter.com/1/statuses/update.xml";

            if (UserToken == null || UserTokenSecret == null)
            {
                SendDebugMessage("Error: Please specify the user token information before attempting to send a tweet.");
                return false;
            }

            ArrayList parameters = new ArrayList();
            parameters.Add(new QueryParameter("status", message));
            parameters.Add(new QueryParameter("trim_user", "1"));

            using (HttpRequest httpRequest = OAuth.GetOAuthRequest(sendTweetUrl, "POST", parameters, UserToken, UserTokenSecret))
            {
                try
                {
                    HttpResponse response = httpRequest.GetResponse();
                    if (response.ResponseCode != 200)
                    {
                        StreamReader reader = response.ResponseStreamReader;
                        do
                        {
                            SendDebugMessage(reader.ReadLine());
                        } while (!reader.EndOfStream);
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

    }
}
